package com.yang.demo.controller;


import com.yang.demo.entity.Admin;
import com.yang.demo.entity.RespBean;
import com.yang.demo.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "个人中心功能接口")
@RestController
@RequestMapping("")
public class AdminInfoController {
    @Autowired
    private IAdminService adminService;

    @ApiOperation("更新当前用户信息")
    @PutMapping("/update/adminInfo")
    public RespBean updateAdminInfo(@RequestBody Admin admin, Authentication authentication) {
        if (adminService.updateById(admin)) {
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin, null, authentication.getAuthorities()));
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败,请重试");
    }

    @ApiOperation("更新用户密码")
    @PutMapping("/update/Password")
    public RespBean updatePassword(@RequestBody Map<String, Object> info) {
        String oldPass = (String) info.get("oldPass");
        String pass = (String) info.get("pass");
        String adminId = (String) info.get("adminId");
        return adminService.updateAdminPassword(oldPass, pass, adminId);
    }

//    @ApiOperation(value = "更新头像")
//    @PutMapping("/update/userface")
//    public RespBean updateUserFace(MultipartFile file,Integer id,Authentication authentication){
//        String[] filepath = FastDFSUtils.uploadFile(file);
//        String url = FastDFSUtils.getTrackerUrl() + filepath[0] + "/" + filepath[1];
//        return adminService.updateAdminUserFace(url,id,authentication);
//    }
}
